/**
 * @param {string} s
 * @param {string[]} words
 * @return {number[]}
 */
export var findSubstring = function (s, words) {
  const wordMap = new Map()
  let cntMap = new Map()
  let [m, n] = [words.length, words[0].length]
  for (const word of words) {
    // 初始化wordMap
    if (wordMap.has(word)) wordMap.set(word, wordMap.get(word) + 1)
    else wordMap.set(word, 1)
  }
  let i = 0
  const ans = []
  let tempIdx = 0
  let wordsCnt = 0
  while (i + n <= s.length && tempIdx + m * n <= s.length) {
    const w = s.slice(i, i + n)
    const cnt = (cntMap.get(w) || 0) + 1
    if (!wordMap.has(w) || cnt > wordMap.get(w)) {
      wordsCnt = 0
      cntMap = new Map()
      tempIdx++
      i = tempIdx
      continue
    }

    if (cnt <= wordMap.get(w)) i += n

    cntMap.set(w, cnt)
    wordsCnt++
    // 当tempList长度刚好等于words长度时，说明完全匹配
    if (wordsCnt === words.length) {
      ans.push(tempIdx)
      wordsCnt = 0
      cntMap = new Map()
      tempIdx++
      i = tempIdx
    }
  }
  return ans
}
